/**
 * 测试object类
 */

public class test11_14_2 extends Object {
    String name;
    String pwd;
    int id;

    test11_14_2() {
    }

    test11_14_2(int id, String name, String pwd) {
        this.id = id;
        this.name = name;
        this.pwd = pwd;
    }
    @Override
    public String toString() {
        return "ID:" + id + "\t" + "账户名:" + name + "\t" + "密码:" + pwd;
    }


    
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((pwd == null) ? 0 : pwd.hashCode());
        result = prime * result + id;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        test11_14_2 other = (test11_14_2) obj;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (pwd == null) {
            if (other.pwd != null)
                return false;
        } else if (!pwd.equals(other.pwd))
            return false;
        if (id != other.id)
            return false;
        return true;
    }

    public static void main(String[] args) {
        test11_14_2 to_1 = new test11_14_2();
        System.out.println(to_1.toString());
        test11_14_2 to_2 = new test11_14_2();
        System.out.println(to_2.toString());
        System.out.println(to_2); // 直接输出对象，默认调用toString()方法

        test11_14_2 t_1 = new test11_14_2(0001, "admin", "123456");
        test11_14_2 t_2 = new test11_14_2(0002, "admin", "123456");
        System.out.println(t_1);
        System.out.println(t_2);
        System.out.println(t_1.equals(t_2)); // 比较两个对象是否相等，默认比较地址
    }

    
}

/*
 * 学习笔记：
 * object类:java中所有类的父类
 * toString()方法：返回该对象的字符串表示
 * hashCode()方法：返回该对象的哈希码值
 * equals()方法：判断两个对象是否相等
 * equals()方法重写：
 * 1.重写equals()方法时，必须重写hashCode()方法
 * 2.重写equals()方法时，必须重写toString()方法
 */